package fserv.entities;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Represents the "FileProposition" message of the Chat System communication protocol.
 * 
 * @author Arthur Bit-Monnot <abit@etud.insa-toulouse.fr>
 * @author Julien Marchand <jmarchan@etud.insa-toulouse.fr>
 */
public class FileProposition extends Message {
    private static final byte MSG_ID = 15;
    
    private String filename;
    private long size;
    private int port;
    
    public FileProposition(String filename, long size, int port) {
        this.filename = filename;
        this.size = size;
        this.port = port;
    }
    
    public FileProposition(InputStream stream) throws InvalidMsgIdException, IOException {
        DataInputStream dis = new DataInputStream(stream);
        byte msgId = dis.readByte();
        if (msgId == MSG_ID) {
            this.filename = dis.readUTF();
            this.size = dis.readLong();
            this.port = dis.readInt();
        } else {
            throw new InvalidMsgIdException(msgId);
        }
    }

    public String getFileName() {
        return this.filename;
    }

    public long getSize() {
        return this.size;
    }

    public int getPort() {
        return this.port;
    }

    @Override
    public void writeToStream(OutputStream stream) throws IOException {
        DataOutputStream dos = new DataOutputStream(stream);
        dos.writeByte(MSG_ID);
        dos.writeUTF(this.filename);
        dos.writeLong(this.size);
        dos.writeInt(this.port);
    }
}